<?php

require_once dirname(__FILE__) . '/../LiteTest.php';

/**
 * Url test case.
 */
class Lite_LiteUrlTest extends PHPUnit_Framework_TestCase {
	/**
	 * @var Url
	 */
	private $Url;
	
	/**
	 * Prepares the environment before running a test.
	 */
	protected function setUp() {
		parent::setUp ();
		$this->url = new LiteUrl();
	}
	
	/**
	 * Cleans up the environment after running a test.
	 */
	protected function tearDown() {
		$this->url = null;
		parent::tearDown();
	}
		
	/**
	 * Tests LiteUrl::fromString()
	 */
	public function testParseString() {
		$scheme = 'http';
		$username = 'username';
		$password = 'password';
		$hostname = 'hostname';
		$path = '/path';
		$query = 'arg=value';
		$fragment = 'anchor';
		
		$url = LiteUrl::parseString("{$scheme}://{$username}:{$password}@{$hostname}{$path}?{$query}#{$fragment}");
		
		$this->assertEquals( $scheme, 	(string) $url['scheme'] );
		$this->assertEquals( $hostname,	(string) $url['host'] );
		$this->assertEquals( $username,	(string) $url['user'] );
		$this->assertEquals( $password,	(string) $url['pass'] );
		$this->assertEquals( $path, 	(string) $url['path'] );
		$this->assertEquals( $query,	(string) $url['query'] );
		$this->assertEquals( $fragment, (string) $url['fragment'] );		
	}
	
	/**
	 * Tests Url->__construct()
	 */
	public function test__constructNoParams() {
		$this->url->__construct();
		$stringUrl = (string) $this->url; 
		$this->assertEquals( '/', $stringUrl );
	}
	
	/**
	 * Tests Url->getDefaultPort()
	 */
	public function testGetDefaultPort() {
		
		$this->assertNull( $this->url->getDefaultPort() );
		
		$this->url->setScheme( LiteUrl::SCHEME_HTTP );
		
		$this->assertEquals( LiteUrl::PORT_HTTP, $this->url->getDefaultPort());
		
		$this->url->setScheme( LiteUrl::SCHEME_HTTPS );
		
		$this->assertEquals( LiteUrl::PORT_HTTPS, $this->url->getDefaultPort());
	}
	
	/**
	 * Tests Url->__toString()
	 */
	public function test__toString() {
		$scheme = 'http';
		$host = 'example.org';
		$path = '/path/to/resource.html';
		$query = 'a=1&b=2';
		$fragment = 'head';
		
		$stringUrl = "{$scheme}://{$host}{$path}?{$query}#{$fragment}";
		
		$this->url->setScheme( $scheme );
		$this->url->setHost( $host );
		$this->url->setPath( $path );
		$this->url->setQuery( $query );
		$this->url->setFragment( $fragment);
		
		$this->assertEquals( $stringUrl, $this->url->__toString() );
		$this->assertEquals( $stringUrl, (string) $this->url );
	}

}

